import 'package:intl/intl.dart';
class TimeUtils{

  //时间 = 》 09:12
  static String getHourMinute(int time){
    return new DateFormat('HH:mm').format(DateTime.fromMillisecondsSinceEpoch(time * 1000));
  }

  //获取当前时间 年-月-日
  static String getCurrentYearMonthDay(){
    DateFormat format = new DateFormat('yyyy-MM-dd');
    return format.format(DateTime.now());
  }

  //获取当前时间 时：分
  static String getCurrentHourMinute(){
    DateFormat format = new DateFormat('HH:mm:ss');
    return format.format(DateTime.now());
  }

  //时间转换成时间戳
  static int timeConvertTimeStamp(int timeStamp){
    var format = new DateFormat('yyyy-MM-dd');
    var date = new DateTime.fromMillisecondsSinceEpoch(
        (timeStamp + 24 * 60 * 60) * 1000);
    var time = '';
    time = format.format(date);
    var dt = DateTime.parse(time);
    return dt.millisecondsSinceEpoch ~/ 1000;
  }

  //获取当前的时间
  static int getCurrentMillion(){
    return DateTime.now().millisecondsSinceEpoch ~/ 1000;
  }

  //年月日
  static String fromMillionToDate(int million){
    DateTime d = new DateTime.fromMicrosecondsSinceEpoch(million * 1000);
    return (d.year.toString() + '年' +d.month.toString() + '月' + d.day.toString() + '日') ;
  }

  //时分
  static String fromMillionToTime(int million){
    DateTime d = new DateTime.fromMicrosecondsSinceEpoch(million);
    return (d.hour.toString()+ ':' +d.minute.toString()) ;
  }

  //月日时分
  static String toMonthDayHourMinute(int timeStamp){
    var format = new DateFormat('yyyy-MM-dd HH:mm');
    var date = new DateTime.fromMillisecondsSinceEpoch(timeStamp * 1000);
    var time = '';
    time = format.format(date);
    return time;
  }

  //月日
  static String getDate(DateTime date) {
    var day = date.day;
    var month = date.month;
    return month.toString() + '月' + day.toString() + '日' ;
  }

  //星期
  static String getWeek(DateTime date){
    var week = date.weekday;
    String w = '';
    switch (week.toString()) {
      case '1':
        w = '一';
        break;
      case '2':
        w = '二';
        break;
      case '3':
        w = '三';
        break;
      case '4':
        w = '四';
        break;
      case '5':
        w = '五';
        break;
      case '6':
        w = '六';
        break;
      case '7':
        w = '日';
        break;
    }
    return '周' + w.toString();
  }

  //身份证获取年 月 日
  static String getYear(String code){
    return code.substring(6,10);
  }
  static String getMonth(String code){
    return code.substring(10,12);
  }
  static String getDay(String code){
    return code.substring(12,14);
  }
  static int getSex(String code){
    return int.parse(code.substring(16,17)) % 2 == 0 ? 0:1; //0女 1男
  }
  static int getAge(String code){
    // 先截取到字符串中的年、月、日
    int selectYear = int.parse(code.substring(6,10));
    int selectMonth = int.parse(code.substring(10,12));
    int selectDay = int.parse(code.substring(12,14));
    // 得到当前时间的年、月、日
    int yearNow = DateTime.now().year;
    int monthNow = DateTime.now().month;
    int dayNow = DateTime.now().day;

    // 用当前年月日减去生日年月日
    int yearMinus = yearNow - selectYear -1;
    int monthMinus = monthNow - selectMonth;
    int dayMinus = dayNow - selectDay;
    int age = yearMinus;
    if (yearMinus < 0) {// 选了未来的年份
      age = 0;
    } else if (yearMinus == 0) {// 同年的，要么为1，要么为0
      if (monthMinus < 0) {// 选了未来的月份
        age = 0;
      } else if (monthMinus == 0) {// 同月份的
        if (dayMinus < 0) {// 选了未来的日期
          age = 0;
        } else if (dayMinus >= 0) {
          age = 1;
        }
      } else if (monthMinus > 0) {
        age = 1;
      }
    } else if (yearMinus > 0) {
      if (monthMinus < 0) {// 当前月>生日月
      } else if (monthMinus == 0) {// 同月份的，再根据日期计算年龄
        if (dayMinus < 0) {
        } else if (dayMinus >= 0) {
          age = age + 1;
        }
      } else if (monthMinus > 0) {
        age = age + 1;
      }
    }
    return age;
  }

  //是否是闰年
  static bool isLunar(int year){
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){  //平闰年判断算法
      return true;
    }return false;
  }
}
